//------------------------------------------------------------------------------
//  TestRenderApp.cpp
//  (C) 2009 xoyojank
//------------------------------------------------------------------------------
#include "StdAfx.h"
#include "TestRenderApp.h"

using namespace Eddy;

//------------------------------------------------------------------------------
bool
TestRenderApp::Open()
{
	if (RenderApplication::Open())
	{
		// vertex layout
		VertexElement vertexElement(VertexElement::Position, 0, VertexElement::Float3);
		std::vector<VertexElement> vertexElements;
		vertexElements.push_back(vertexElement);
		VertexLayoutPtr vertexLayout = new VertexLayout(vertexElements);

		// vertex buffer
		VertexBufferPtr vertexBuffer = new VertexBuffer(VertexBuffer::Static);
		float vertices[] = { 0.0f,  0.5f, 0.0f, 
							-0.5f, -0.5f, 0.0f,
							 0.5f, -0.5f, 0.0f };
		vertexBuffer->SetSize(sizeof(vertices));
		vertexBuffer->Write(0, sizeof(vertices), vertices);
		vertexBuffer->SetVertexLayout(vertexLayout);
		
		// primitive group
		PrimitiveGroup primitiveGroup;
		primitiveGroup.SetNumVertices(3);
		primitiveGroup.SetTopologyType(PrimitiveGroup::TriangleList);
		std::vector<PrimitiveGroup> primitiveGroups;
		primitiveGroups.push_back(primitiveGroup);

		// mesh
		this->mesh = new Mesh();
		this->mesh->SetVertexBuffer(vertexBuffer);
		this->mesh->SetPrimitiveGroup(primitiveGroups);
		this->mesh->ApplyPrimitiveGroup(0);

		// effect
		this->effect = EffectManager::Instance()->CreateEffect(L"shader\\colored.vp.bin",
			L"shader\\colored.fp.bin");
		const EffectVariablePtr& varShapeColor = this->effect->GetVariable("ShapeColor");
		varShapeColor->SetVector(float4(0, 1, 0, 1));

		return true;
	}

	return false;
}

//------------------------------------------------------------------------------
void
TestRenderApp::Close()
{
	this->mesh = NULL;
	RenderApplication::Close();
}

//------------------------------------------------------------------------------
void
TestRenderApp::OnRenderFrame()
{
	this->renderDevice->Clear();

	this->renderDevice->BeginPass(this->effect);
	this->renderDevice->Draw();
	this->renderDevice->EndPass();

	this->renderDevice->Present();
}